<?php
!defined('M_P') && exit('Forbidden');

/**
 * 出售房module
 */
class PW_HouseSaleModule {
	
	var $subject;//标题
	var $uid;//用户名
	var $username;//用户名
	var $istop;//是否置顶-0 为不置顶
	var $isurgent;//是否加急:0表示不加急
	var $isbroker;//是否是经济人
	var $acreage;//总面积
	var $currentfloor;//当前楼层
	var $totalfloor;//总楼层
	var $price;//总售价
	var $unitprice;//单位面积价格
	var $room;//户型：室/厅/卫/阳台
	var $righter;//房屋产权
	var $village;//小区名
	var $plate;//板块
	var $address;//具体地址
	var $buildtime;//建筑年代
	var $traffic;//交通情况
	var $fitment;//装修程度
	var $equipment;//配套设施
	var $thumb;//缩略图
	var $state;//状态
	var $descrip;//房源描述
	var $gate;//组合信息供搜索用
	var $posttime; //发布时间
	var $modifytime; //修改时间
	var $attachs;//图片
	var $areatype;//区域类型
	var $housetype;//物业类型
	var $linkman; //联系人信息
	var $tel;//电话
	var $phone;//手机
	var $email;//邮箱
	var $_filterUitil;//词语过滤
	
	function __construct() {
		$this->_filterUitil = L::loadClass('FilterUtil', 'filter');
	}
	
	function PW_HouseSaleModule() {
		$this->__construct();
	}

	function setUid($uid) {
		$uid = (int) $uid;
		if(!$uid){
			Error::addError('请填写用户ID');
		}
		$this->uid = $uid;
	}
	
	function setSubject($subject){
		if (($banword = $this->_filterUitil->comprise($subject)) !== false) {
			Showmsg('警告：内容中含有非法词语 \''.$banword.'\'！');
		}
		$subject  = trim($subject);
		if(strlen($subject)<=0 || !$subject){
			Error::addError('房源标题不能为空');
		}
		if (strlen($subject) > 100) {
			Error::addError('房源标题不能超过100字节');
		}
		$this->subject = $subject;
	}
	
	function setUsername($username) {
		$userService = L::loadClass('userservice','user');
		$userInfo = $userService->getByUserName($username);
		if (!$userInfo) Error::addError('该用户不存在');
		$this->username = $username;
		$this->setUid($userInfo['uid']);
	}

	function setIstop($istop) {
		$istop = (int) $istop;
		$this->istop = $istop;
	}
	function setIsurgent($isurgent) {
		$this->isurgent = (int)$isurgent;
	}
	
	function setIsbroker($isbroker){
		$isbroker = $isbroker ? 1 : 0;
		$this->isbroker = $isbroker;
	}
	
	function setAcreage($acreage) {
		(!$acreage || !preg_match('/^\d+(\.{0,1}\d+){0,1}$/',$acreage)) && Error::addError("建筑面积是必填项,并且必须是数字或小数");
		$this->acreage = $acreage;
	}

	function setCurrentfloor($currentfloor) {
		$currentfloor = (int)$currentfloor;
		$this->currentfloor = $currentfloor;
	}
	
	function setTotalfloor($totalfloor) {
		$totalfloor = (int)$totalfloor;
		$this->totalfloor = $totalfloor;
	}
	
	function setPrice($price) {
		$price = (float)$price;
		(!$price || !preg_match('/^\d+(\.{0,1}\d+){0,1}$/',$price)) && Error::addError("出售总价为必填项,并且必须是数字");
		$this->price = $price;
	}

	function setRoom($room) {
		$this->room = $room;
	}
	
	function setThumb($thumb){
		$this->thumb = $thumb;
	}
	
	function setAttachs($oldatt_desc = array(),$keep=array()) {
		global $db_allowupload, $_G;
		$attachs = $this->attachs ? $this->attachs : array();
		$attachs = $this->_cookOldAttachs($attachs, $oldatt_desc, $keep);
		House::loadClass('ImageUpload', 'upload', false);
		$uploaddb = array();
		if ($db_allowupload && $_G['allowupload']) {
			$imageUpload = new ImageUpload('sale');
			$uploaddb = PwUpload::upload($imageUpload, false);
			$uploaddb && $this->thumb = $imageUpload->getFirstThumbPic($uploaddb);
			foreach ($uploaddb as $key => $value) {
				$value = $this->_fliterAttach($value);
				$uploaddb[$key] = $value;			
			}
		}
		$this->attachs = (array)$attachs + (array)$uploaddb;
		
	}
	function setRighter($righter) {
		$righter = (int)$righter;
		!$righter && Error::addError('请选择房屋产权');
		$this->righter = $righter;
		
	}
	function setVillage($village) {
		!trim($village) && Error::addError('请填写小区名称');
		$this->village = $village;
	}
	
	function setPlate($plate) {
		$this->plate = (int)$plate;
	}
	
	function setAddress($address) {
		!trim($address) && Error::addError('请填写具体地址');
		$this->address = $address;
	}
	
	function setBuildtime($buildtime) {
		$buildtime = (int)$buildtime;
		$this->buildtime = $buildtime;
	}
	
	function setTraffic($traffic){
		$this->traffic = $traffic;
	}
	
	function setFitment($fitment){
		$fitment = (int)$fitment;
		$this->fitment = $fitment;
	}
	
	function setEquipment($equipment){
		$equipment && !S::isArray($equipment) && Error::addError('填写配套设施有误');
		$equipment = implode(',',$equipment);
		$this->equipment = $equipment;
	}

	function setState($state){
		$state = (int)$state;
		$this->state = (int)$state;
	}
	
	function setDescrip($descrip){
		if (($banword = $this->_filterUitil->comprise($descrip)) !== false) {
			Showmsg('警告：内容中含有非法词语 \''.$banword.'\'！');
		}
		if (strlen(trim($descrip)) > 50000) {
			Error::addError('描述内容过长');
		}
		$this->descrip = Char_cv($descrip);	
	}
	
	function setGate($gate){
		$this->gate = $gate;
	}
	
	function setPosttime($posttime) {
		$this->posttime = (int)$posttime;
	}
	
	function setModifytime($modifytime) {
		$this->modifytime = (int)$modifytime;
	}
	
	function setAreatype($areatype){
		$areatype = (int)$areatype;
		!$areatype && Error::addError('请选择所属区域');
		$this->areatype = $areatype;
	}
	
	function setHousetype($housetype){
		$housetype = (int)$housetype;
		$this->housetype = $housetype;
	}
	
	function setLinkMan($linkman,$isbroker = false){
		$linkman = trim($linkman);
		if(!$isbroker && !$linkman){
			Error::addError('请填写联系人');
		}
		$this->linkman = $linkman;
	}
	
	function setTel($tel){
		$this->tel = $tel;
	}
	
	function setPhone($phone){
		$this->phone = $phone;
	}
	
	function setEmail($email){
		$this->email = $email;
	}
	
	function _fliterAttach($attach) {
		global $windid, $timestamp;
		if (!S::isArray($attach)) return array();
		$attach['type'] = 1;
		$attach['size'] = $attach['size'];
		$attach['path'] = $attach['fileuploadurl'];
		$attach['uploadtime'] = $timestamp;
		$attach['descrip'] = Char_cv(GetGP('atc_desc_'.$attach['id']));
		$attach['creator'] = $windid;
		return $attach;
	}
	
	function _cookOldAttachs($attachs, $oldatt_desc, $keep) {
		foreach ($attachs as $key => $value) {
			if (!in_array($value['imageid'], $keep)) {
				$value['attname'] = 'delete';
			} else {
				$value['descrip'] = $oldatt_desc[$value['imageid']];
				$value['attname'] = 'replace';
			}
			$attachs[$key] = $value;
		}
		return $attachs;
	}
	
	function checkTelOrPhone($isbroker = false){
		if(!$isbroker && !$this->tel && !$this->phone){
			Error::addError('电话或者手机号码任填一项');
		}
	}
	
	function checkFloor(){
		if((int)$this->currentfloor > (int)$this->totalfloor){
			Error::addError('当前楼层不能大于总楼层');
		}
	}

}